<template>
  <div class="member-order-page">
  <!-- tab组件 -->
    <XtxTabs v-model="activeName" @tab-click="tabClick">
      <XtxTabsPanel
        v-for="item in orderStatus"
        :key="item.name"
        :label="item.label"
        :name="item.name">
        </XtxTabsPanel>
    </XtxTabs>
    <!-- 订单列表 -->
    <div class="order-list">
     <div v-if="loading" class="loading"></div>
     <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem 
      v-for="item in orderList" 
      :key="item.id" 
      :order="item" 
      @on-cancel-order="onCancelOrder(item)"
      @on-delete-order="handlerDelete(item)"
      @on-confirm-order="onConfirmOrder(item)"
      />
    </div>
    <!-- 分页组件 -->
  <XtxPagination
      v-if="total > 0"
      @current-change="requestParams.page=$event"
      :total="total"
      :page-size="requestParams.pageSize"
      :current-page="requestParams.page"  />
      <!-- 取消原因组件 -->
      <OrderCancel ref="orderCancelCom" />
      <!-- 查看物流组件 -->
      <OrderLogistics ref="logisticsOrderCom" />
  </div>
</template>
<script>
import { ref ,reactive,watch} from 'vue'
import { orderStatus } from '@/api/constants'
import { findOrderList,deleteOrder, confirmOrder} from '@/api/order'
import OrderItem from './components/order-item'
import OrderCancel from './components/order-canel'
import Confirm from '@/components/library/confirm'
import Message from '@/components/library/Message'
import OrderLogistics from './components/order-logisics'
export default {
  name: 'MemberOrderPage',
  components: { OrderItem ,OrderCancel,OrderLogistics},
  setup () {
    const activeName = ref('all')
    // 查询订单参数
    const requestParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0
    })
    // 订单列表
    const orderList = ref([])
    const total = ref(0)
    const loading = ref(false)
    const getOrderList = () => {
      loading.value = true
      findOrderList(requestParams).then(data => {
        orderList.value = data.data.result.items
        total.value = data.data.result.counts
        loading.value = false
      })
    }
    // 筛选条件变化重新加载
    watch(requestParams, () => {
       getOrderList()
    }, { immediate: true })
    // 点击选项卡
    const tabClick = ({ index }) => {
      requestParams.page = 1
      requestParams.orderState = index
    }
     // 删除订单
    const handlerDelete = (order) => {
      Confirm({ text: '亲，您确认删除该订单吗？' }).then(() => {
        deleteOrder(order.id).then(() => {
          Message({ type: 'success', text: '删除成功' })
          getOrderList()
        })
      }).catch(() => {})
    }
      return {
      activeName,
      orderStatus,
      tabClick,
      requestParams,
      orderList,
      loading,
      total,
      handlerDelete,
      ...useCancelOrder(),
      ...useConfirmOrder(),
      ...useLogisticsOrder()
    }
  }
}
// 封装逻辑-取消订单
export const useCancelOrder = () => {
  const orderCancelCom = ref(null)
  const onCancelOrder = (item) => {
    // item 就是你要取消的订单
    orderCancelCom.value.open(item)
  }
  return { onCancelOrder, orderCancelCom }
}
// 封装逻辑-确认收货
export const useConfirmOrder = () => {
  const onConfirmOrder = (item) => {
    // item 就是你要确认收货的订单
    Confirm({ text: '您确认收到货吗？确认后货款将会打给卖家。' }).then(() => {
      confirmOrder(item.id).then(() => {
        Message({ text: '确认收货成功', type: 'success' })
        // 确认收货后状态变成 待评价
        item.orderState = 4
      })
    })
  }
  return { onConfirmOrder }
}
// 封装逻辑-查看物流
export const useLogisticsOrder = () => {
  const logisticsOrderCom = ref(null)
  const onLogisticsOrder = (item) => {
    logisticsOrderCom.value.open(item)
  }
  return { onLogisticsOrder, logisticsOrderCom }
}
</script>
<style scoped lang="less">
.order-list {
  padding: 20px;
  position: relative;
  min-height: 400px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255,255,255,.9) url(../../../assets/images/loading.gif) no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>